<?php
App::import('Model','Assets.Asset');
class DealAsset extends Asset 
{
	var $name = 'DealAsset';
	
	const PUBLISHED = 1;
	const UNPUBLISHED = 0;
	var $scopes = array(
		'published' => array('DealAsset.status'=>self::PUBLISHED)
	);
	var $order = array("DealAsset.created"=>"ASC");
	var $belongsTo = array(
						'Deal'=>array(
								'className' => 'Deals.Deal',
								'foreignKey' => 'deal_id',
								'dependent' => true,
								'conditions' => '',
								'fields' => '',
								'order' => '',
							)
					);	

	var $actsAs = array('Containable');
					
	function afterInitialize()
	{
		$isImageRequired = false;
		if($isImageRequired)
		{
			$empty = false;
		}else
		{
			$empty = true;
		}		
		$uploadConfig = array("name"=> array('dir'=>WWW_ROOT.'uploads'.DS.'deal_assets'.DS,'mediaTypes'=>array("Image"),'empty'=>$empty));				
		$this->Behaviors->attach('Upload',$uploadConfig);
		$this->additionalFields[] = "file_url";
		parent::afterInitialize();
	}
	
	function beforeSave()
	{		
		$return = parent::beforeSave();
		if(!isset($this->data['DealAsset']['name']))
		{
			unset($this->data['DealAsset']);
		}
		return $return;	
	}
	
	function unpublishAssociatedEntity($idsToUpdate = null,$foreignKey = null)
	{
		if(!empty($foreignKey))
		{
			$idsToUpdate = $this->find('list',array('fields'=>array('id'),'conditions'=>array($this->alias.'.'.$foreignKey=>$idsToUpdate)));
			$this->updateAll(array($this->alias.'.status'=>self::UNPUBLISHED),array($this->alias.'.id'=>$idsToUpdate));
		}
		$this->refershCounters($idsToUpdate);
		
		$belongsToAssociations = $this->belongsTo;
		if(!empty($belongsToAssociations))
		{
			foreach($belongsToAssociations as $parentModel=>$assoc)
			{
				$foreignKey = $assoc['foreignKey'];
				$keys[$foreignKey] = $this->field($foreignKey,array($this->alias.'.id'=>$idsToUpdate));
				$this->updateCounterCache($keys);
			}
		}
		
		$hasManyAssociations = $this->hasMany;
		if(!empty($hasManyAssociations))
		{
			foreach($hasManyAssociations as $childModel=>$config)
			{
				if(method_exists($this->{$childModel},'unpublishAssociatedEntity'))
				{
					$this->{$childModel}->unpublishAssociatedEntity($idsToUpdate,$config['foreignKey']);
				}
			}
		}		
	}
	
	function file_url($asset)
	{
		if(isset($asset[$this->alias]['name']))
		{
			return '/uploads/deal_assets/'.$asset[$this->alias]['name'];
		}
		return false;
	}
	
	function beforeChildrenSave($data,$DealModel)
	{
		$userId = null;
		foreach($data as $key => $attachment)
		{
			if(!isset($attachment['name']) || !$attachment['name']['name'])
			{
				unset($data[$key]);
			} else {
				$data[$key]['status'] = self::PUBLISHED;
			}
		}
		return $data;
	}
	
	
	function makeAsDefault($dealId,$dealAssetId)
	{
		$dealAssetId = $this->field($this->alias.".id",array($this->alias.".id"=>$dealAssetId,$this->alias.".deal_id"=>$dealId));
		if($dealAssetId)
		{
			$this->updateAll(array('is_default'=>0),array('DealAsset.deal_id'=>$dealId));
			$this->id = $dealAssetId;
			$this->saveField('is_default',1,false,false);
			return true;
		}
		return false;
	}
				
}
?>